package com.woniuxy.jiapei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.vo.OrderQuery;
import com.woniuxy.jiapei.pojo.*;
import com.woniuxy.jiapei.service.*;
import com.woniuxy.jiapei.util.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RequestMapping;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-22
 */
@CrossOrigin
@RestController
@RequestMapping("/jiapei/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalDeptService hospitalDeptService;
    @Autowired
    BedService bedService;
    @Autowired
    CustomerService customerService;

    @ApiOperation(value = "查询")
    @GetMapping(value = "/select/{id}")
    public R selectok(@PathVariable("id") Integer id) {
        Orders order = ordersService.getById(id);
        if (order != null) {
            System.out.println("成功");
            return R.ok().data("list", order).message("成功");

        } else {
            return R.error();
        }

    }

    //    @ApiOperation(value = "订单查询")
//    @GetMapping(value = "/selectAll")
//    public R selectoklist(){
//        List<OrderDisplay> orderList = ordersService.getOrderList();
//        if (orderList != null) {
//            return R.ok().data("list",orderList).message("成功");
//        } else {
//            return R.error();
//        }
//
//
//    }
    @ApiOperation(value = "订单查询")
    @GetMapping(value = "/selectAll1")
    public R selectlist(@ApiParam(value = "当前页") @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
                        @ApiParam(value = "每页的记录数") @RequestParam(value = "limit", required = true, defaultValue = "5") Integer limit, OrderQuery orderQuery) throws ParseException {
        Page pageBean = new Page<>(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        //获取医院和科室信息
        //医院集合
        List<Hospital> hospitals = hospitalService.list();
        //科室集合
        List<HospitalDept> hospitalDepts = hospitalDeptService.list();
//        List<Orders> list = ordersService.list();
        if (orderQuery.getOrderNumber() != null && orderQuery.getOrderNumber() != 0) {
            queryWrapper.eq("order_number", orderQuery.getOrderNumber());
        }
        if (!StringUtils.isEmpty(orderQuery.getHosName())) {
            queryWrapper.eq("hos_name", orderQuery.getHosName());
        }
        if (!StringUtils.isEmpty(orderQuery.getHosTypeName())) {
            queryWrapper.eq("hos_type_name", orderQuery.getHosTypeName());
        }
        if (orderQuery.getOrderStatus() != null && orderQuery.getOrderStatus() != 0) {
            queryWrapper.eq("order_status", orderQuery.getOrderStatus());
        }
        //默认查询没有被删除的订单
        queryWrapper.eq("status", 1);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (!StringUtils.isEmpty(orderQuery.getOrdderTime1())) {
            if (!StringUtils.isEmpty(orderQuery.getOrdderTime2())) {
                System.out.println(orderQuery.getOrdderTime1());
                System.out.println(orderQuery.getOrdderTime2());
                Date date1 = format.parse(orderQuery.getOrdderTime1());
                System.out.println(date1);
                Date date2 = format.parse(orderQuery.getOrdderTime2());
                Calendar cal = Calendar.getInstance();
                cal.setTime(date2);
                cal.add(Calendar.DATE, 1);
                date2 = cal.getTime();
                System.out.println(date2);
                queryWrapper.between("ordder_time", date1, date2);
            }
        }
        Page resultPage = ordersService.page(pageBean, queryWrapper);
        List records = resultPage.getRecords();
//        List recordes = resultPage.getOrders();
//        System.out.println(recordes);
        Long total = resultPage.getTotal();
//        List<OrderDisplay> orderList = ordersService.getOrderList();


        if (records != null) {
            return R.ok().data("recordes", records).data("total", total).data("hospitals", hospitals).data("hospitalDepts", hospitalDepts);
        } else {
            return R.error().message("没有集合");
        }
    }

    @ApiOperation("按照id删除订单信息")
    @GetMapping(value = "/orders/{id}")
    public R delectOrders(@PathVariable("id") Integer id) {
        Orders orders = ordersService.getById(id);
//        QueryWrapper queryWrapper = new QueryWrapper();

//        queryWrapper.eq("status",0);
//        boolean b = ordersService.saveOrUpdate(orders, queryWrapper);
//        boolean b = ordersService.update(orders, queryWrapper);
//        boolean b = ordersService.save(orders);
//        boolean b = ordersService.removeById(id);
        orders.setStatus(0);
        boolean b = ordersService.saveOrUpdate(orders);
        if (b) {
            return R.ok().message("成功");
        } else {
            return R.error().message("失败");
        }

    }


    @ApiOperation("查看详情")
    @GetMapping(value = "/display/{id}")
    public R diplay(@PathVariable("id") Integer id) {
        //订单信息
        Orders orders = ordersService.getById(id);
        //床位信息
        Bed bed = bedService.getById(orders.getBedId());
        //顾客信息
        Customer customer = customerService.getById(orders.getCusId());
        return R.ok().data("orders", orders).data("bed", bed).data("customer", customer);

    }
}

